﻿#pragma once
#if defined(_MSC_VER)
    #define _CRT_SECURE_NO_WARNINGS
#endif
//#include <stdio.h>
//#include <time.h>
#include <windows.h>
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <array>
#include <algorithm> // for std::sort / std::find / std::find_if / std::count / std::for_each
#include <functional> // for std::function
//using std::cout; // 命名空间声明
//using std::endl;
//using std::string;
//using std::array;
using namespace std; // 命名空间声明，这样所有的 std:: 都不需要加前缀了，也可以加上，不建议在头文件中使用这个，这里只是演示

class Device {
private:
    string m_name;
public:
    Device()
        : m_name("None") {
    }
    Device(string name)
        : m_name(name) {
    }

    string get_name() const {
        return m_name;
    }
    void set_name(string name) {
        m_name = name;
        cout << "Device name changed to " << m_name << endl;
    }
};

class DeviceManager {
private:
    using my_dm_type = std::unordered_map<string, std::vector<Device*>>;
    //typedef std::unordered_map<string, std::vector<Device*>> my_dm_type; // 另一种写法
    my_dm_type my_devices;
public:
    const std::vector<Device*>& get_device(string name) const {
        return my_devices.at(name);
    }

    my_dm_type& get_devices() {
        return my_devices;
    }

    void add_device(string& name, Device*& device) {
        my_devices[name].push_back(device);
        cout << "Device " << device->get_name() << " added to " << name << endl;
    }

    void remove_device(string& name, Device*& device) {
        auto& devices = my_devices[name];
        for (auto it = devices.begin(); it != devices.end(); ++it)
            if (*it == device) {
                devices.erase(it);
                cout << "Device " << device->get_name() << " removed from "
                    << name << " success" << endl;
                return;
            }
        cout << "Device " << device->get_name() << " not found in " << name << endl;
    }
};

extern void test_macro_auto();
extern void test_namespace();



// 宏定义
#define print_start(str) std::cout << " ----------------- " << str << " Start ------------------------\n";
#define print_end(str) std::cout << " ----------------- " << str << " End ------------------------\n\n\n";